// pages/write-off/traveler-input.js
const app = getApp()
Page({


    data: {
        travelerList: []
    },
    onLoad: function(options) {
        this.setData(options)
        if (app.travelerList) {
            this.setData({
                travelerList: app.travelerList
            })
        }
        if (app.orderConfig) {
            this.setData(app.orderConfig)
        }
    },
    validate(key, value) {
        this.setData({
            [`${key}_start`]: true
        })
        if (key == "mobile") { //手机号校验
            let reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
            this.setData({
                [`${key}`]: reg.test(value) && value ? value : '',
            })
        } else if (key == "idCard") { //身份证校验
            let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
            this.setData({
                [`${key}`]: reg.test(value) && value ? value : '',
            })
        } else {
            this.setData({
                [`${key}`]: value ? value : '',
            })
        }
    },
    bindinput(evt) {
        let key = evt.currentTarget.dataset.key
        let value = evt.detail.value
        this.validate(key, value)
    },

    submit(evt) {
        let travelerList = this.data.travelerList
        let obj = evt.detail.value
        let k = null
        Object.keys(obj).forEach((key) => {
            this.validate(key, obj[key])
            if (obj[key] == "" && !k) {
                k = true
                return
            }
        });
        if (k || !this.data.object_id) {
            return
        }
        wx.showLoading({
            title: '请稍等',
        })
        let traveler = {
            object_id: this.data.object_id,
            consignee: this.data.name,
            tel: this.data.mobile,
            id_card: this.data.idCard,
        }
        app.http.post('hx-goods/update-member-object', {
            data: traveler
        }).then(res => {
            wx.hideLoading()
            let page = getCurrentPages()
            let prePage = page[page.length - 2]
            wx.navigateBack({})
        }).catch(err => {
            wx.hideLoading()
            app.wxToast.error(err.message)
        })
    }
})